package com.inoneone.toolkit.exception;

import java.util.regex.Pattern;

/**
 *
 * ClassName: AssertUtil <br/>
 * Function: 断言. <br/>
 * date: 2016年9月9日 下午12:03:56 <br/>
 *
 * @author 姬远玄
 * @version
 * @since JDK 1.7
 */
public abstract class AssertUtil {

    /**
     * 断言一定要为true，否则报错
     *
     * @param expression
     *            表达式
     * @param message
     *            消息
     */
    public static void isTrue(boolean expression, String message, String errorCode) {
        if (!expression) {
            throw new BusinessRuntimeException(errorCode, String.format("%s", message));
        }
    }

    /**
     * 判断某对像必须是null，如果不null报错
     *
     * @param object
     * @param field
     *            字段名字，方便查看问题
     */
    public static void isNull(Object object, String field, String errorCode) {
        if (object != null) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某对像不能为null，如果为null则报错
     *
     * @param object
     * @param field
     */
    public static void notNull(Object object, String field, String errorCode) {
        if (object == null) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某字符串不能为NULL or ""。如果等于null,""都会报错
     *
     * @param text
     *            字符串值
     * @param field
     *            字符串名称
     */
    public static void hasText(String text, String field, String errorCode) {
        if (isEmpty(text)) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    public static boolean isEmpty(String str) {
        return (str == null || "".equals(str.trim()));
    }

    /**
     * 判断某字符串长度必须大于最小值小于最大值
     *
     * @param text
     *            字符串值
     * @param field
     *            字符串名称
     * @param minLen
     *            最小长度
     * @param maxLen
     *            最大长度
     */
    public static void hasText(String text, String field, int minLen, int maxLen, String errorCode) {
        notNull(text, field, errorCode);
        if (text.length() < minLen || text.length() > maxLen) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某字符串必须大于等于某长度
     *
     * @param text
     *            字符串值
     * @param field
     *            字符串名称
     * @param minLen
     *            最小长度
     */
    public static void hasTextAndMoreThan(String text, String field, int minLen, String errorCode) {
        hasText(text, field, errorCode);
        if (text.length() < minLen) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某字符串长度必须大于最小值小于最大值
     *
     * @param text
     *            字符串值
     * @param field
     *            字符串名称
     * @param fixLen
     *            固定长度
     */
    public static void hasText(String text, String field, int fixLen, String errorCode) {
        hasText(text, field, errorCode);
        if (text.length() != fixLen) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某数组不能为空
     *
     * @param array
     * @param message
     */
    public static void notEmpty(Object[] array, String message, String errorCode) {
        if (isEmpty(array)) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     *
     * isEmpty:(数组是否为空). <br/>
     *
     * @author 姬远玄
     * @param array
     * @return
     * @since JDK 1.7
     */
    public static boolean isEmpty(Object[] array) {
        return (array == null || array.length == 0);
    }

    /**
     * 判断二个short一定要相等，不等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isEqualTo(short src, short dest, String message, String errorCode) {

        if (src != dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个int一定要相等，不等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isEqualTo(int src, int dest, String message, String errorCode) {

        if (src != dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个long一定要相等，不等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isEqualTo(long src, long dest, String message, String errorCode) {

        if (src != dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个double一定要相等，不等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isEqualTo(double src, double dest, String message, String errorCode) {

        if (src != dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个int一定要不相等，相等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isNotEqualTo(int src, int dest, String message, String errorCode) {

        if (src == dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个short一定要不相等，相等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isNotEqualTo(short src, short dest, String message, String errorCode) {

        if (src == dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个long一定要不相等，相等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isNotEqualTo(long src, long dest, String message, String errorCode) {

        if (src == dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断二个double一定要不相等，相等报错
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isNotEqualTo(double src, double dest, String message, String errorCode) {

        if (src == dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断第一个数大于第二个数
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isGreateThan(double src, double dest, String message, String errorCode) {

        if (src <= dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断第一个数大于第二个数
     *
     * @param src
     * @param dest
     * @param message
     */
    public static void isGreateThan(long src, long dest, String message, String errorCode) {

        if (src <= dest) {
            throw new BusinessRuntimeException(errorCode, String.format(message));
        }
    }

    /**
     * 判断某值必须在二个区间之间
     *
     * @param field
     * @param value
     * @param min
     * @param max
     */
    public static void isBetween(int value, String field, int min, int max, String errorCode) {

        if (value < min || value > max) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某值必须在二个区间之间
     *
     * @param field
     * @param value
     * @param min
     * @param max
     */
    public static void isBetween(long value, String field, long min, long max, String errorCode) {
        if (value < min || value > max) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 判断某值是否满足正则表达式
     *
     * @param field
     */
    public static void isMatch(String text, String field, String pattern, String errorCode) {
        hasText(text, field, errorCode);
        if (!Pattern.matches(pattern, text)) {
            throw new BusinessRuntimeException(errorCode, String.format("参数[%s]错误", field));
        }
    }

    /**
     * 断言一定要为true，否则报错
     *
     * @param expression
     *            表达式
     */
    public static void isTrue(boolean expression, ExceptionModel exceptionModel) {
        if (!expression) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某对像必须是null，如果不null报错
     *
     * @param object
     */
    public static void isNull(Object object, ExceptionModel exceptionModel) {
        if (object != null) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某对像不能为null，如果为null则报错
     *
     * @param object
     */
    public static void notNull(Object object, ExceptionModel exceptionModel) {
        if (object == null) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某字符串不能为NULL or ""。如果等于null,""都会报错
     *
     * @param text
     *            字符串值
     */
    public static void hasText(String text, ExceptionModel exceptionModel) {
        if (isEmpty(text)) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某字符串长度必须大于最小值小于最大值
     *
     * @param text
     *            字符串值
     * @param minLen
     *            最小长度
     * @param maxLen
     *            最大长度
     */
    public static void hasText(String text, int minLen, int maxLen, ExceptionModel exceptionModel) {
        notNull(text, exceptionModel);
        if (text.length() < minLen || text.length() > maxLen) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某字符串必须大于等于某长度
     *
     * @param text
     *            字符串值
     * @param minLen
     *            最小长度
     */
    public static void hasTextAndMoreThan(String text, int minLen, ExceptionModel exceptionModel) {
        hasText(text, exceptionModel);
        if (text.length() < minLen) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某字符串长度必须大于最小值小于最大值
     *
     * @param text
     *            字符串值
     * @param fixLen
     *            固定长度
     */
    public static void hasText(String text, int fixLen, ExceptionModel exceptionModel) {
        hasText(text, exceptionModel);
        if (text.length() != fixLen) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某数组不能为空
     *
     * @param array
     */
    public static void notEmpty(Object[] array, ExceptionModel exceptionModel) {
        if (isEmpty(array)) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }


    /**
     * 判断二个short一定要相等，不等报错
     *
     * @param src
     * @param dest
     */
    public static void isEqualTo(short src, short dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src != dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断二个int一定要相等，不等报错
     *
     * @param src
     * @param dest
     */
    public static void isEqualTo(int src, int dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src != dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断二个long一定要相等，不等报错
     *
     * @param src
     * @param dest
     */
    public static void isEqualTo(long src, long dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src != dest) {
            throw new BusinessRuntimeException( exceptionModel);
        }
    }

    /**
     * 判断二个double一定要相等，不等报错
     *
     * @param src
     * @param dest
     */
    public static void isEqualTo(double src, double dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src != dest) {
            throw new BusinessRuntimeException( exceptionModel);
        }
    }

    /**
     * 判断二个int一定要不相等，相等报错
     *
     * @param src
     * @param dest
     */
    public static void isNotEqualTo(int src, int dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src == dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断二个short一定要不相等，相等报错
     *
     * @param src
     * @param dest
     */
    public static void isNotEqualTo(short src, short dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src == dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断二个long一定要不相等，相等报错
     *
     * @param src
     * @param dest
     */
    public static void isNotEqualTo(long src, long dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src == dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断二个double一定要不相等，相等报错
     *
     * @param src
     * @param dest
     */
    public static void isNotEqualTo(double src, double dest,ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);
        if (src == dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断第一个数大于第二个数
     *
     * @param src
     * @param dest
     */
    public static void isGreateThan(double src, double dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);

        if (src <= dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断第一个数大于第二个数
     *
     * @param src
     * @param dest
     */
    public static void isGreateThan(long src, long dest, ExceptionModel exceptionModel) {
        notNull(src, exceptionModel);
        notNull(dest, exceptionModel);

        if (src <= dest) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某值必须在二个区间之间
     *
     * @param value
     * @param min
     * @param max
     */
    public static void isBetween(int value, int min, int max, ExceptionModel exceptionModel) {
        notNull(value, exceptionModel);
        if (value < min || value > max) {
            throw new BusinessRuntimeException(exceptionModel);
        }
    }

    /**
     * 判断某值必须在二个区间之间
     *
     * @param value
     * @param min
     * @param max
     */
    public static void isBetween(long value, long min, long max, ExceptionModel exceptionModel) {
        notNull(value, exceptionModel);
        if (value < min || value > max) {
            throw new BusinessRuntimeException( exceptionModel);
        }
    }

    /**
     * Description: 判断某值必须在二个区间之间</br>
     * Date: 2017-06-27 14:15:37</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    public static void isBetween(double value, double min, double max, ExceptionModel exceptionModel) {
        notNull(value, exceptionModel);
        if (value < min || value > max) {
            throw new BusinessRuntimeException( exceptionModel);
        }
    }

    /**
     * 判断某值是否满足正则表达式
     *
     */
    public static void isMatch(String text, String pattern, ExceptionModel exceptionModel) {
        hasText(text, exceptionModel);
        if (!Pattern.matches(pattern, text)) {
            throw new BusinessRuntimeException(exceptionModel.getErrCode(), exceptionModel.getErrMsg());
        }
    }

}
